<?php

/**
 * Функция компилятора Smarty для вставки блока на страницу.
 *
 * @param   string          $tag_attrs
 * @param   Smarty_Compiler $compiler
 * @return  string
 */
function smarty_compiler_format($tag_attrs, &$compiler) {

    $params = $compiler->_parse_attrs($tag_attrs);

    if (!isset($params['default']))
    {
        $compiler->_syntax_error("block: missing 'default' parameter", E_USER_WARNING);
        return false;
    }

    if (!isset($params['template'])) {
        $compiler->_syntax_error("block: missing 'template' parameter", E_USER_WARNING);
        return false;
    }

    $output = 'if ($this->template_exists(' . $params['template'] .')) {'
        . '$_smarty_tpl_vars = $this->_tpl_vars;'
        . '$this->_smarty_include(array("smarty_include_tpl_file" => ' . $params['template'] . ', "smarty_include_vars" => array()));'
        . '$this->_tpl_vars = $_smarty_tpl_vars;'
        . 'unset($_smarty_tpl_vars);'
        . '} else {'
        . 'echo ' . $params['default'] .';'
        . '}';

    return $output;
}
